package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.AddressBean;
import com.hxkcloud.nurse.dborm.AddressDao;
import com.hxkcloud.nurse.popup.AddressSelectorPopWindow;

/**
 * Created by william_sun on 2016-07-06.
 * Description:注册用户个人信息填写(身高体重地址)
 */
public class UserAddressActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private EditText et_province;
    private EditText et_city;
    private EditText et_area;
    private Button bt_sure;

    private boolean provinceInput = false;
    private boolean cityInput = false;
    private boolean areaInput = false;

    private String provinceId = "";
    private String cityId = "";
    private String areaId = "";

    private AddressSelectorPopWindow addressPop;

    private AddressDao addressDao;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_useraddress);
        initView();
        addressDao = new AddressDao(mContext);
        Intent iGet = this.getIntent();
        if (iGet.hasExtra("provincename")) {
            et_province.setText(iGet.getStringExtra("provincename"));
            et_city.setText(iGet.getStringExtra("cityname"));
            et_area.setText(iGet.getStringExtra("areaname"));
        }
        initView();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        et_province = (EditText) findViewById(R.id.et_useraddress_province);
        et_city = (EditText) findViewById(R.id.et_useraddress_city);
        et_area = (EditText) findViewById(R.id.et_useraddress_area);
        bt_sure = (Button) findViewById(R.id.bt_useraddress_sure);

        setTitlePadding(ll_title);
        iv_back.setOnClickListener(this);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getString(R.string.useraddress));

        et_province.setFocusable(false);
        et_city.setFocusable(false);
        et_area.setFocusable(false);
        et_province.setOnClickListener(this);
        et_city.setOnClickListener(this);
        et_area.setOnClickListener(this);
        bt_sure.setOnClickListener(this);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.bt_useraddress_sure:
                if (areaInput) {
                    String province = et_province.getText().toString();
                    String city = et_city.getText().toString();
                    String area = et_area.getText().toString();
                    Intent intent = new Intent();
                    intent.putExtra("province", province);
                    intent.putExtra("city", city);
                    intent.putExtra("area", area);
                    intent.putExtra("areaId", areaId);
                    setResult(1, intent);
                    finish();
                } else {
                    toastMsg(R.string.pleaseinputwholecontent);
                }
                break;
            case R.id.et_useraddress_province:
                if (addressPop == null) {
                    addressPop = new AddressSelectorPopWindow(this);

                }

                addressPop.setAddressListener(new AddressSelectorPopWindow.AddressListener(
                ) {
                    @Override
                    public void setAddress(AddressBean address) {
                        et_province.setText(address.getName());
                        provinceId = address.getItemId();

                        provinceInput = true;

                        et_city.setText("");
                        et_area.setText("");
                        cityInput = false;
                        areaInput = false;
                        cityId = "";
                        areaId = "";

                        bt_sure.setBackgroundResource(R.drawable.bt_grey);
                        bt_sure.setEnabled(false);
                    }
                });
                addressPop.show(ll_title, addressDao.getAddress("0"));
                break;
            case R.id.et_useraddress_city:
                if (!provinceInput) {
                    Toast.makeText(mContext, R.string.selectprovince, Toast.LENGTH_LONG).show();
                } else {
                    if (addressPop == null) {
                        addressPop = new AddressSelectorPopWindow(this);
                    }
                    addressPop.setAddressListener(new AddressSelectorPopWindow.AddressListener(
                    ) {
                        @Override
                        public void setAddress(AddressBean address) {
                            et_city.setText(address.getName());
                            cityId = address.getItemId();

                            cityInput = true;

                            et_area.setText("");
                            areaInput = false;
                            areaId = "";

                            bt_sure.setBackgroundResource(R.drawable.bt_grey);
                            bt_sure.setEnabled(false);
                        }
                    });
                    addressPop.show(ll_title, addressDao.getAddress(provinceId));
                }
                break;
            case R.id.et_useraddress_area:
                if (!cityInput) {
                    Toast.makeText(mContext, R.string.selectcity, Toast.LENGTH_LONG).show();
                } else {
                    if (addressPop == null) {
                        addressPop = new AddressSelectorPopWindow(this);
                    }
                    addressPop.setAddressListener(new AddressSelectorPopWindow.AddressListener(
                    ) {
                        @Override
                        public void setAddress(AddressBean address) {
                            et_area.setText(address.getName());
                            areaId = address.getItemId();

                            areaInput = true;

                            bt_sure.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                            bt_sure.setEnabled(true);
                        }
                    });
                    addressPop.show(ll_title, addressDao.getAddress(cityId));
                }
                break;
        }
    }
}
